<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->

<template>
  <div class="pull-height animated" :class="{'zoomOutUp': isLock}">
    <div class="index">
      <sidebar class="left"></sidebar>
      <div class="right">
        <div class="nav">
          <div ref="firstLogin" style="display: none">{{userInfo.firstLogin}}</div>
          <top class="top">
          </top>
          <tags></tags>
        </div>
        <div class="main">
          <div class="router">
            <keep-alive>
              <router-view v-if="$route.meta.keepAlive"></router-view>
            </keep-alive>
            <router-view v-if="!$route.meta.keepAlive"></router-view>
          </div>
        </div>
      </div>
    </div>
    <el-dialog :visible.sync="dialogFormVisible" :close-on-press-escape="false" :close-on-click-modal="false" :show-close="false">
      <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-width="100px" >
        <!--<el-form-item label="用户名" prop="username">-->
        <!--<el-input type="text" :value="userInfo.username" disabled></el-input>-->
        <!--</el-form-item>-->
        <el-form-item label="原密码" prop="password">
          <el-input type="password" v-model="ruleForm2.password"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="newpassword1">
          <el-input type="password" v-model="ruleForm2.newpassword1"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="newpassword2">
          <el-input type="password" v-model="ruleForm2.newpassword2"></el-input>
        </el-form-item>
        <!--<el-form-item label="手机号" prop="phone">-->
        <!--<el-input v-model="userInfo.phone" placeholder="验证码登录使用"></el-input>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="头像">-->
        <!--<my-upload field="file" @crop-upload-success="cropUploadSuccess" v-model="show" :width="300" :height="300" url="/admin/user/upload" :headers="headers" img-format="png"></my-upload>-->
        <!--<img :src="userInfo.avatar">-->
        <!--<el-button type="primary" @click="toggleShow" size="mini">选择-->
        <!--<i class="el-icon-upload el-icon&#45;&#45;right"></i>-->
        <!--</el-button>-->
        <!--</el-form-item>-->
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
          <el-button @click="resetForm('ruleForm2')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import tags from "./tags";
import top from "./top/";
import sidebar from "./sidebar/";
import request from "@/router/axios";
export default {
  components: {
    top,
    tags,
    sidebar
  },
  name: "index",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else if (value.length < 6) {
        callback(new Error('密码不能小于6位'));
      } else {
        // if (this.ruleForm2.newpassword1 !== '') {
        //   this.$refs.ruleForm2.validateField('newpassword1');
        // }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm2.newpassword1) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      dialogFormVisible: false,
      ruleForm2: {
        password: '',
        newpassword1: '',
        newpassword2: ''
      },
      rules2: {
        newpassword1: [{ validator: validatePass, trigger: 'blur' }],
        newpassword2: [{ validator: validatePass2, trigger: 'blur' }]
      }
    };
  },
  created() {},
  mounted() {
    if (this.$refs.firstLogin.innerText == 1) {
      this.dialogFormVisible = true;
    }
  },
  computed: {
    ...mapState({
      userInfo: state => state.user.userInfo
    }),
    ...mapGetters(["isLock"])
  } ,
  props: [],
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          request({
            url: "/admin/user/editInfo",
            method: "put",
            data: this.ruleForm2
          }).then(response => {
            if (response.data.data) {
              this.$notify({
                title: "成功",
                message: "修改成功",
                type: "success",
                duration: 2000
              });
              this.dialogFormVisible = false
              // 修改密码之后强制重新登录
              if (this.ruleForm2.newpassword1 !== "") {
                this.$store.dispatch("LogOut").then(() => {
                  location.reload(); // 为了重新实例化vue-router对象 避免bug
                });
              } else {
                this.$router.push({ path: "/" });
              }
            } else {
              this.$notify({
                title: "失败",
                message: response.data.msg,
                type: "fail",
                duration: 2000
              });
            }
          }).catch(() => {
            this.$notify({
              title: "失败",
              message: "修改失败",
              type: "fail",
              duration: 2000
            });
          });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang="scss" scoped>
.index {
  display: flex;
  position: relative;
  height: 100%;
  background: #fff;
  overflow: hidden;
  .left {
    width: 230px;
    overflow-y: auto;
  }
  .right {
    padding-top: 107px;
    position: relative;
    height: 100%;
    box-sizing: border-box;
    overflow: hidden;
    flex: 1;
  }
  .main {
    height: 100%;
    background: #f1f4f5;
    padding: 15px;
    box-sizing: border-box;
    overflow-y: scroll;
    .router {
      padding: 15px 8px;
      background: #fff;
      min-height: 100%;
      height: auto;
      overflow: hidden;
      border-radius: 5px;
      box-sizing: border-box;
    }
  }
  .nav {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 999;
  }
  .top {
    margin-bottom: 2px;
  }
}
</style>
